#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll a[N];
int mn[N], mx[N], topn, topx;
int ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  mn[++topn] = 1, mx[++topx] = 1;
  rep(i, 2, n) {
    while (topx && a[mx[topx]] < a[i]) topx--;
    int x = topx ? mx[topx] : 0;
    mx[++topx] = i;
    while (topn && a[mn[topn]] >= a[i]) topn--;
    if (topn) {
      int y = lower_bound(mn + 1, mn + topn + 1, x + 1) - mn;
      if (y <= topn) ans = max(ans, i + 1 - mn[y]);
    }
    mn[++topn] = i;
  }
  cout << ans;
  return 0;
}